package com.million.educenter.controller;


import com.million.commonutils.JwtUtils;
import com.million.commonutils.R;
import com.million.commonutils.orderVo.UcenterMemberVo;
import com.million.educenter.entity.UcenterMember;
import com.million.educenter.entity.params.vo.LoginParams;
import com.million.educenter.entity.params.vo.RegisterVo;
import com.million.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-04-03
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("loginUser")
    public R loginUser (@RequestBody LoginParams loginParams) {
        String token = ucenterMemberService.loginUser(loginParams);
        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("registerUser")
    public R registerUser (@RequestBody RegisterVo registerVo) {
        ucenterMemberService.registerUser(registerVo);
        return R.ok();
    }

    //得到会员信息 解析token
    @GetMapping("getMemberInfo")
    public R getMemberInfo (HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember memberInfo = ucenterMemberService.getById(memberId);
        return R.ok().data("memberInfo", memberInfo);
    }

    //根据用户id 查询用户信息
    @GetMapping("getMemberInfoById/{memberId}")
    public UcenterMemberVo getMemberInfoById (@PathVariable("memberId") String memberId) {
        UcenterMemberVo ucenterMember = ucenterMemberService.getMemberInfoById(memberId);
        return ucenterMember;
    }

    //根据创建时间查询注册人数
    @GetMapping("getRegisterUcenter/{day}")
    public Integer getRegisterUcenter (@PathVariable("day") String day) {
        return ucenterMemberService.getRegisterUcenter(day);
    }
}

